/*
 * @Description: 
 * @version: 
 * @Author: 171
 * @Date: 2022-08-31 11:27:31
 * @LastEditors: 171
 * @LastEditTime: 2022-09-22 18:03:50
 */
import axios from 'axios';
import qs from 'qs';
// import CryptoClass from "./aes";
import md5 from 'js-md5';


// const loginRegisterCrypto = new CryptoClass('03a4df791e6c28b5', 'f57cd2401ab698e3');

export const server = axios.create({
  baseURL: 'http://localhost:5555'

})

server.interceptors.request.use(function(config){ // 请求拦截器
  // 在 headers 中设置authorization 属性放token，token是存在缓存中
  config.headers!.authorization = localStorage.getItem("token") ?? ""; 
  return config;
}, function (error) {
  return Promise.reject(error);
});

const getMyApiValidate = async () => {
  const publicKey = localStorage.getItem("publicKey") ?? "";
  const response: any = await server.post('/myapi/validate', {  publicKey });
  const dataStr = response.data;
  const { code, token }: any = qs.parse(dataStr); 
  const isValid = code == 0 ? true : false
  
  return Promise.resolve(isValid);
}

export const getApiList = () => {
  return server.get('/otherapi/epidemiclist');
}

export const postMyApiLogin = (paramsObj: any, success: Function, fail: Function) => {
  paramsObj.account = md5(paramsObj.account);
  paramsObj.password = md5(paramsObj.password);
  const reqData = qs.stringify(paramsObj);
  return server.post(`/myapi/user/login`, {
    reqData
  }).then((response) => {
    if(response.status === 200){
      const resData: any = qs.parse(response.data);
      if(resData.code == 0){
        const { token, publicKey, id } = resData;
        success(token, publicKey, id);
      }
      if(resData.code == 1) {
        fail();
      }
    }
  })
}

export const postMyApiRegister = (paramsObj: object, success: Function, fail: Function) => {
  const reqData = qs.stringify(paramsObj);
  return server.post(`/myapi/user/register`, {
    reqData
  }).then((response) => {
    if(response.status === 200){
      const resData = qs.parse(response.data);
      if(resData.message === "ok"){
        success();
      }
      if(resData.message === "error"){
        fail();
      }
    }
  })
}


export const getMyApiArticle = async (page: number, number = 5, callback: Function) => {
  const isValid = await getMyApiValidate();
  if(!isValid) return; 
  const id  = localStorage.getItem('id')!;
  const reqData = qs.stringify({ 
    id,
    page,
    number
  });
  return server.get(`/myapi/article`, {
    params: {
      reqData
    }
  }).then((response) => {
    if(response.status === 200){
      const resData = qs.parse(response.data);
      callback(resData.data);
    }
  })
}

export const postMyApiArticle = async (paramsObj: any, success: Function) => {
  const isValid = await getMyApiValidate();
  if(!isValid) return; 
  const { title, content, description, publishTime } = paramsObj;
  const id = localStorage.getItem('id')!;
  console.log(id)
  const reqData = qs.stringify({ 
    userId: id,
    title,
    content,
    description,
    publishTime
  });
  return server.post(`/myapi/article`, {
    reqData
  }).then((response) => {
    if(response.status === 200){
      const resData = qs.parse(response.data);
      if(resData.message === "ok"){
        success();
      }
      if(resData.message === "error"){
        console.log("error", resData.error)
      }
    }
  })
}
